package cn.hongyu.controller;

import cn.hongyu.pojo.User;
import cn.hongyu.pojo.VO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

@Controller
public class UserController {

    @RequestMapping("/hellotest")
    String test1() {
        return "hello";
    }

    //1.获得基本类型参数
    @RequestMapping("/request1")
    @ResponseBody
    //http://localhost:8999/request1?username=jak&age=18
    void request1(String username,int age){
        System.out.println(username);
        System.out.println(age);
    }
    //1.2获取集合以 Ajax请求 Json格式
    //http://localhost:8999/request6?name=张三
    @RequestMapping("/request6")
    @ResponseBody
    void request6(@RequestParam(value = "name")String username){
        System.out.println(username);
    }
    //2.获得pojo类型参数
    //http://localhost:8999/request2?username=jllk&age=99
    @RequestMapping("/request2")
    @ResponseBody
    void request2(User user){
        System.out.println(user);
    }

    //3.获取数组
    @RequestMapping("/request3")
    @ResponseBody
    //http://localhost:8999/request3?stars=aaa&stars=123&stars=bbb
    void request3(String[] stars){
        System.out.println(stars);
        System.out.println(Arrays.asList(stars));
        System.out.println("---------------");
        System.out.println(Arrays.toString(stars));
        System.out.println("---------------");
        for(String star:stars){
            System.out.println(star);
        }
    }

    //4.1获取集合(将集合包装到pojo进行封装)
    //先进入from表单
    //action="${pageContext.request.contextPath}/request4"获取目录下的request4
    @RequestMapping("/request4")
    @ResponseBody
    void request4(VO vo){
        System.out.println(vo);
    }

    //4.2获取集合以 Ajax请求 Json格式
    @RequestMapping("/request5")
    @ResponseBody
    void request5(@RequestBody List<User> list){
        System.out.println(list);
    }

}
